<template>
  <div class="root">
    <el-main>
      <!-- <Header @updateCollapse = "updateCollapse"></Header> -->
      <div class="mainTab" v-if="$route.meta.showTag">
        <el-row>
          <el-col class="xiala">
            <div class="con" @click="isOpen">
              <i
                :class="
                  collapse
                    ? 'iconcebianlan-zhankai icon iconfont'
                    : 'iconcebianlan-shouqi icon iconfont'
                "
              ></i>
            </div>
          </el-col>
          <el-col :span="24" class="tabs">
            <el-tabs
              @tab-click="tabClick"
              v-model="activeIndex"
              type="border-card"
              @edit="handleTabsEdit"
              class="barTag"
            >
              <el-tab-pane
                :closable="index == '0' ? false : true"
                :key="item.url"
                v-for="(item, index) in sliderBar"
                :label="item.name"
                :newName="item.newName"
                :name="item.url"
              >
                <span slot="label" v-if="!isYS()"
                  ><i
                    class="icon iconfont iconshouye"
                    v-show="item.name == '首页'"
                  ></i>
                  {{ item.name == '首页' ? '' : item.name }}
                </span>
                <span slot="label" v-else
                  ><i
                    class="icon iconfont iconshouye"
                    v-show="
                      item.name == '首页' &&
                      item.url != '/home/monitorPage/constructMonitorHome'
                    "
                  ></i>
                  {{
                    item.name == '首页' &&
                    item.url == '/home/monitorPage/constructMonitorHome'
                      ? '首页'
                      : item.name == '首页' &&
                        item.url != '/home/monitorPage/constructMonitorHome'
                      ? ''
                      : item.name
                  }}
                </span>
              </el-tab-pane>
            </el-tabs>
          </el-col>
          <el-col class="xiala">
            <!-- <i class="icon iconfont iconxiala"></i> -->
            <el-dropdown>
              <div class="con">
                <!-- <p class="el-dropdown-link"> -->
                <i class="icon iconfont iconxiangxia"></i>
              </div>
              <!-- </p> -->
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="closeTab('current')">
                  <i class="icon iconfont icon-user"></i>
                  <span class="icon-span">关闭当前标签页</span>
                </el-dropdown-item>
                <el-dropdown-item @click.native="closeTab('other')">
                  <i class="icon iconfont icon-edit-square"></i>
                  <span class="icon-span">关闭其它标签页</span>
                </el-dropdown-item>
                <el-dropdown-item @click.native="closeTab('all')">
                  <i class="icon iconfont icon-edit-square"></i>
                  <span class="icon-span">关闭全部标签页</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </el-row>
      </div>

      <!-- <router-view></router-view> -->
      <transition name="move" mode="out-in">
        <keep-alive :include="keepAlive">
          <router-view
            class="containerRouterView containerRouterViewWrap"
          ></router-view>
        </keep-alive>
        <!-- <router-view ></router-view> -->
      </transition>
    </el-main>
  </div>
</template>
<script>
import Header from '@/components/publicCom/Header'
export default {
  name: 'Main',
  components: {
    Header,
  },
  data() {
    return {
      activeIndex: '/home',
      collapse: false, //左侧菜单控制展开隐藏
    }
  },
  // props: ["menuList"],
  mounted() {},
  computed: {
    editableTabsValue() {
      return this.$store.getters.geteditableTabsValue
    },
    sliderBar() {
      return this.$store.getters.getSliderBar
    },
    keepAlive() {
      return this.$store.getters.keepAlive
    },
    getActiveIndex() {
      return this.$store.state.activeIndex
        ? this.$store.state.activeIndex
        : '/home'
    },
  },
  created() {},
  methods: {
    isOpen() {
      this.collapse = !this.collapse
      this.$store.commit('changeCollapse', this.collapse)
    },
    tabClick(item) {
      if (item.$attrs.newName != undefined) {
        this.$router.push(item.$attrs.newName)
      } else {
        this.$router.push(item.name)
      }

      this.$store.state.activeMenu = item.name
      // console.log(item)
    },
    handleTabsEdit(targetName, action) {
      if (action === 'remove') {
        this.$store.commit('DELETE_SLIDER', targetName)
      }
    },
    updateCollapse(data) {
      // 从子组件传过来的值，再传到父组件
      this.$emit('updateCol', data)
    },
    closeTab(type) {
      if (this.$store.state.sliderBar.length == 1) {
        this.$message({
          showClose: true,
          message: '暂无可关闭的标签页',
          type: 'warning',
          duration: 1500,
        })
        return
      }
      //关闭全部
      if (type == 'all') {
        this.$store.commit('CLOSE_TAB', type)
      } else {
        // 如果是选中的是第一个，不能删除
        if (type == 'other') {
          // if(this.$store.state.sliderBar[0].url != this.$store.state.activeMenu){
          //关闭其他
          if (this.$store.state.sliderBar.length < 3) {
            this.$message({
              showClose: true,
              message: '暂无其他标签页',
              type: 'warning',
              duration: 1500,
            })
            return
          }
          this.$store.commit('CLOSE_TAB', type)
          // }else{
          //   this.$message({
          //       showClose: true,
          //       message: "暂无可关闭的标签页",
          //       type: "warning",
          //       duration: 1500
          //   });
          // }
        } else {
          if (
            this.$store.state.sliderBar[0].url != this.$store.state.activeMenu
          ) {
            // 关闭当前
            this.$store.commit('DELETE_SLIDER', this.$store.state.activeMenu)
          } else {
            this.$message({
              showClose: true,
              message: '暂无可关闭的标签页',
              type: 'warning',
              duration: 1500,
            })
          }
        }

        return
        if (
          this.$store.state.sliderBar[0].url != this.$store.state.activeMenu
        ) {
          if (type == 'other') {
            //关闭其他
            if (this.$store.state.sliderBar.length == 2) {
              this.$message({
                showClose: true,
                message: '暂无其他标签页',
                type: 'warning',
                duration: 1500,
              })
              return
            }
            this.$store.commit('CLOSE_TAB', type)
          } else {
            // 关闭当前
            this.$store.commit('DELETE_SLIDER', this.$store.state.activeMenu)
          }
        } else {
          this.$message({
            showClose: true,
            message: '暂无可关闭的标签页',
            type: 'warning',
            duration: 1500,
          })
        }
      }
    },
  },
  watch: {
    getActiveIndex: {
      handler(val) {
        var url = decodeURI(val)
        var item = this.sliderBar.filter(
          (f) => f.url == url || f.newName == url
        )
        if (item[0]) {
          if (this.$route.fullPath != url) {
            if (item[0].newName != undefined) {
              this.$router.push(item[0].newName)
            } else {
              this.$router.push(item[0].url)
            }
          }
          this.activeIndex = item[0].url
        } else {
          this.activeIndex = '/home'
        }
      },
      deep: true,
      immediate: true, //如果是想watch检测到值变化并且立刻使用则需要加上，刷新页面需要被监听，解决刷新选中的问题。
    },
  },
}
</script>
<style scoped lang="less">
/* main */
.root {
  position: absolute;
  left: 230px;
  top: 64px;
  right: 0;
  bottom: 0;
  // overflow: hidden;
  -webkit-transition: left 0.3s ease-in-out;
  transition: left 0.3s ease-in-out;
  .el-main {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
    // overflow: initial;
    /deep/ .toolbar {
      padding: 16px 0 0;
      background-color: #fff;
      //   margin: 10px 0;
    }
    .el-breadcrumb {
      font-size: 16px;
    }
  }
}

// tab栏样式
.mainTab {
  -webkit-box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12),
    0 0 6px 0 rgba(0, 0, 0, 0.04);
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
  /deep/ .el-tabs--border-card > .el-tabs__header .el-tabs__item.is-active {
    color: #333333;
    font-size: 14px;
    background-color: #fff;
    border-right-color: #e2e3e5;
    border-left-color: #e2e3e5;
    height: 100%;
    z-index: 9999;
  }
}

.mainTab /deep/ .el-tabs--border-card {
  box-shadow: none;
  > .el-tabs__header {
    background-color: #f3f3f3;
    border-bottom: 0;
    // box-shadow: 2px 2px 2px 0 rgba(0,0,0,0.10);
  }
}

.mainTab /deep/ .el-tabs--border-card {
  border-top: none;
  border-bottom: none;
  // box-shadow: 2px 2px 2px 0 rgba(0, 0, 0, 0.1);
}
.mainTab
  /deep/
  .el-tabs--border-card
  > .el-tabs__header
  .el-tabs__item
  + .el-tabs__item,
.el-tabs--border-card > .el-tabs__header .el-tabs__item:first-child {
  margin-left: 0px;
}

.mainTab /deep/ .el-tabs__item {
  text-align: center;
  padding: 0px 12px;
  font-size: 14px;
  font-weight: 0 !important;
}
.mainTab /deep/ .el-tabs__header .el-tabs__item.is-active:first-child {
  padding: 0px 12px;
}

.mainTab /deep/ .el-tabs--border-card > .el-tabs__header .el-tabs__item {
  font-size: 14px;
  color: #666;
  background-color: #f3f3f3;
  border-right: 0.5px solid #e2e3e5;
}

.mainTab /deep/ .el-tabs--border-card > .el-tabs__header .el-tabs__item:hover {
  color: #333333;
  background-color: #f9f9f9;
}
.mainTab /deep/ .el-tabs__header .el-tabs__item:nth-child(2) {
  padding-left: 12px !important;
}
.mainTab /deep/ .el-tabs__header .el-tabs__item:last-child {
  padding-right: 12px !important;
}
.mainTab /deep/ .el-tabs__nav-prev,
.mainTab /deep/ .el-tabs__nav-next {
  line-height: 36px;
  width: 36px;
  height: 36px;
  .el-icon-arrow-left {
    font-family: 'iconfont' !important;
    font-size: 16px;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
  .el-icon-arrow-left:before {
    line-height: 36px;
    content: '\e61c';
    font-size: 12px;
  }

  .el-icon-arrow-right {
    font-family: 'iconfont' !important;
    font-size: 16px;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
  .el-icon-arrow-right:before {
    line-height: 36px;
    content: '\e61e';
    font-size: 12px;
  }
}

.mainTab /deep/ .el-tabs__nav-prev {
  border-right: 1px solid #e2e3e5;
}
.mainTab /deep/ .el-tabs__nav-prev:hover {
  background-color: #f9f9f9;
}

.mainTab /deep/ .el-tabs__nav-next {
  border-left: 1px solid #e2e3e5;
}
.mainTab /deep/ .el-tabs__nav-next:hover {
  background-color: #f9f9f9;
}

.mainTab /deep/ .el-tabs__nav-wrap.is-scrollable {
  padding: 0 36px;
  text-align: center;
  // line-height: 40px;
  // width: 40px;
  // height: 40px;
}
.mainTab .barTag /deep/.el-tabs__nav-wrap {
  width: 100%;
}
.tabs {
  width: calc(100% - 72px);
}

// 下拉样式
.mainTab /deep/ .el-row {
  .xiala {
    height: 36px;
    width: 36px;
    line-height: 36px;
    text-align: center;
    background-color: #f3f3f3;
    // border-bottom: 1px solid #DCDFE6;
    cursor: pointer;
    // -webkit-box-shadow: 2px 2px 2px 0 rgba(0, 0, 0, 0.1);
    // box-shadow: 2px 2px 2px 0 rgba(0, 0, 0, 0.1);
    /deep/ .iconxiala {
      font-size: 22px;
    }
  }
  .xiala:hover {
    background-color: #f9f9f9;
  }
  .con {
    width: 36px;
    height: 36px;
  }
}
.root .containerRouterView {
  height: calc(100% - 66px);
  position: absolute;
  overflow-y: auto;
  top: 52px;
  left: 16px;
  right: 16px;
  bottom: 16px;
  background-color: #fff;
  //   padding-right: 24px;
}
::-webkit-scrollbar {
  width: 0px;
  /*对垂直流动条有效*/
  height: 7px;
  /*对水平流动条有效*/
}
.mainTab /deep/ .el-tabs__item {
  height: 36px;
  line-height: 36px;
}
/deep/ .el-tabs__item {
  height: 46px;
  line-height: 46px;
}
/deep/ .el-tabs--border-card > .el-tabs__header .el-tabs__item {
  border: 0px;
}
/deep/ .el-tabs__item {
  font-weight: normal !important;
}
/deep/ .el-tabs__nav-wrap {
  margin-bottom: 0px;
}
</style>
